// component/tab/tab.js
const
    app = getApp(),
    globalData = app.globalData,
    utils = require('../../utils/util');

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        tabIndex: {
            type: Number,
            value: 1, //默认推荐
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        isIpx: globalData.isIpx,
        tabs: [
            {
                text: '首页',
                active: false,
                src: globalData.imageBaseUri+'/images/home1.png',
            },
            {
                text: '分类',
                active: false,
                src: globalData.imageBaseUri+'/images/type1.png',
            },
            {
                text: '购物车',
                active: false,
                src: globalData.imageBaseUri+'/images/car1.png',
            },
            {
                text: '我的',
                active: false,
                src: globalData.imageBaseUri+'/images/mine1.png',
            },
        ],
    },

    attached() {
        let tabs = this.data.tabs,
            index = this.data.tabIndex;

        tabs[index].src = tabs[index].src.split('.png')[0] + '-press.png';
        tabs[index].active = true;

        this.setData({ tabs });
    },

    /**
     * 组件的方法列表
     */
    methods: {
        clickTab(e) {
            let index = +e.currentTarget.dataset.index,
                url = '/pages/index/index',
                tab = this.data.tabs[index];

            if (this.data.tabIndex === index) return;

            switch (index) {
                case 1:
                    url = '/pages/classify/classify';
                    break;
                case 2:
                    url = '/pages/car/car';
                    break;
                case 3:
                    url = '/pages/mine/mine';
                    
                    // // 订阅下级支付成功通知
                    // utils.subscribe('cTHEhOYPqcC693tSzZ_1MTbixtbKbXmrL2aGhzMrviw');

                    break;
            }

            // if (!utils.getLoginState() && index === 2) {
            //     return utils.checkLoginState({
            //         routeUri: 3,
            //         uri: '/pages/recommend/recommend'
            //     });
            // }

            //会员商城页面为非tab页，不能使用reLaunch打开
            // index === 1 ? wx.navigateTo({url}) : wx.reLaunch({url});
            wx.reLaunch({ url })
        },

    }
})

// "tabBar": {
//     "custom": true,
//     "color": "#C2C2C2",
//     "selectedColor": "#A6D7C7",
//     "backgroundColor": "#ffffff",
//     "list": [
//         {
//             "pagePath": "pages/index/index",
//             "iconPath": "images/home1.png",
//             "selectedIconPath": "images/home1-press.png",
//             "text": "首页"
//         },
//         {
//             "pagePath": "pages/classify/classify",
//             "iconPath": "images/type1.png",
//             "selectedIconPath": "images/type1-press.png",
//             "text": "分类"
//         },
//         {
//             "pagePath": "pages/car/car",
//             "iconPath": "images/car1.png",
//             "selectedIconPath": "images/car1-press.png",
//             "text": "购物车"
//         },
//         {
//             "pagePath": "pages/mine/mine",
//             "iconPath": "iimages/mine1.png",
//             "selectedIconPath": "images/mine1-press.png",
//             "text": "我的"
//         }
//     ]
// }